package com.erp.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {
    public final static String encode(String str){
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes());
            byte mdBytes[] = md.digest();
            String hash="";
            for (int i=0;i<mdBytes.length;i++){
                int temp;
                if(mdBytes[i]<0){
                    temp=256+mdBytes[i];
                }else{
                    temp=mdBytes[i];
                }
                if(temp<16){
                    hash+="0";
                }
                hash+=Integer.toString(temp,16);
            }
            return hash;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
    public static String encodeWithSalt(String numStr,String salt){
        return encode(encode(numStr)+salt);
    }
    public static void main(String [] args){
        System.out.println(encode("test"));
        System.out.println(encodeWithSalt("123456", "123456"));
    }
}
